<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

include_once("ext_lib/simple_html_dom.php");

function execute_slideshow($site_content){

    global $site_head_extra;

    $html = str_get_html($site_content);



    foreach($html->find("a") as $element){
        if ($element->class == "ekkin" and $element->id == "slideshow"){
            $dir = $element->href;
            $dim = $element->title;

            foreach (scandir($dir) as $file){
                if (str_endswith($file, ".jpg")){
                    $title = rtrim($file, ".jpg");
                    $imgs .=  "[\"$dir/$file\", \"\", \"\", \"$title\"],\n";
                }
            }
            $element->outertext = "<div class=\"slideshow\" id=\"fadeshow\"></div>";
            $imgs = rtrim($imgs, ",\n");

            $script = "
            <script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\"></script>
            <script type=\"text/javascript\" src=\"ext_lib/fadeslideshow.js\"></script>
            <script type=\"text/javascript\">
            var mygallery2=new fadeSlideShow({
            wrapperid: \"fadeshow\", //ID of blank DIV on page to house Slideshow
            dimensions: [$dim], //width/height of gallery in pixels. Should reflect dimensions of largest image
            imagearray: [$imgs],
            displaymode: {type:'auto', pause:6000, cycles:0, wraparound:true},
            persist: false, //remember last viewed slide and recall within same session?
            fadeduration: 500, //transition duration (milliseconds)
            descreveal: \"ondemand\"
        })</script>";
            $site_head_extra .= $script;
        }
    }


    return $html->save();
}

?>


